package com.st.bottomnavigation.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.st.bottomnavigation.R;
import com.st.bottomnavigation.bean.test.Student;
import com.st.bottomnavigation.ui.adapter.GridViewAdapter;
import com.st.bottomnavigation.ui.adapter.test.StudentListAdapter;
import com.st.bottomnavigation.utils.SystemUtils;
import com.st.bottomnavigation.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

public class TaskPhotoActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout takePhoto;
    private static final int REQUEST_CODE_TAKE_PHOTO = 110;//拍照请求code
    private ArrayList<String> imagePaths = new ArrayList<>();//存放图片
    private String mPhotoPath = null;//照片存储路径
    private MyGridView gridView;
    private GridViewAdapter gridViewAdapter;
    private ListView lv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_photo);
        initView();

    }

    private void initView() {
        takePhoto = findViewById(R.id.takePhoto);
        gridView = findViewById(R.id.gridView);
        lv_list = findViewById(R.id.lv_list);
        Button send = findViewById(R.id.send);
        takePhoto.setOnClickListener(this);
        send.setOnClickListener(this);

        List<Student> studentList = getData();//获取数据
        showStudentListView(studentList);//显示数据
    }
    public List<Student> getData(){
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setStuName("学生"+i);
            student.setStuNo(1000+i+"");
            list.add(student);
        }
        return list;
    }
    private StudentListAdapter studentListAdapter;
    private void showStudentListView(List<Student> studentList) {
        if(studentListAdapter == null) {
            //第一次加载
            studentListAdapter = new StudentListAdapter(this);
            studentListAdapter.setData(studentList);
            lv_list.setAdapter(studentListAdapter);// 设置列表视图的适配器
        }else {
            // 非第一次，更新数据
            studentListAdapter.setData(studentList);
            studentListAdapter.notifyDataSetChanged();
        }
    }
    /**
     * @param requestCode 请求code
     * @param resultCode  结果code
     * @param data 数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            //拍照成功回调
            case REQUEST_CODE_TAKE_PHOTO:
                if (!SystemUtils.isEmpty(mPhotoPath)) {
                    imagePaths.add(mPhotoPath);
                    gridViewAdapter = new GridViewAdapter(imagePaths);
                    gridView.setAdapter(gridViewAdapter);
                }
                break;
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.takePhoto:
                gotoTakePhoto();
                break;
            case R.id.send:

                break;
        }
    }
    private void gotoTakePhoto() {
        if (imagePaths != null) {
            if (imagePaths.size() <= 2) {
                mPhotoPath = SystemUtils.takePhotos(TaskPhotoActivity.this, REQUEST_CODE_TAKE_PHOTO);
            } else {
                Toast.makeText(TaskPhotoActivity.this, "最多可上传3张", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * handler处理回调数据
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

            }
        }
    };

}

